package com.ys.service.amazonorder.impl;

import com.alibaba.fastjson.JSON;
import com.bo.vo.R;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.amazonorder.TbBaseinfoSpyt;
import com.ys.mapper.amazonorder.TbBaseinfoSpytMapper;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.service.amazonorder.TbBaseinfoSpytService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("tbBaseinfoSpytService")
public class TbBaseinfoSpytServiceImpl implements TbBaseinfoSpytService {

	@Autowired
	private TbBaseinfoSpytMapper tbBaseinfoSpytMapper;
	@Autowired
	private DiySqlMapper diySqlMapper;
	@Override
	public R add(Map<String, Object> params) {
		TbBaseinfoSpyt entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoSpyt.class);
		String s = "[\\u4e00-\\u9fa5]+";
		if(!entity.getUsageCn().matches(s)){
			return R.error("输入有误,请重新输入");
		}
		String m = "^.{1,50}$";
		if(!entity.getUsageEn().matches(m)){
			return R.error("输入有误,请重新输入");
		}
		if (new TbBaseinfoSpyt().setUsageCn(entity.getUsageCn()).count() > 0) {
			return R.error("该数据已存在");
		}
		if (new TbBaseinfoSpyt().setUsageCn(entity.getUsageEn()).count() > 0) {
			return R.error("该数据已存在");
		}
		tbBaseinfoSpytMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}

	@Override
	public R edit(Map<String, Object> params) {

		TbBaseinfoSpyt entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoSpyt.class);

		String s = "[\\u4e00-\\u9fa5]+";
		if(!entity.getUsageCn().matches(s)){
			return R.error("输入有误,请重新输入");
		}
		String m = "^.{1,50}$";
		if(!entity.getUsageEn().matches(m)){
			return R.error("输入有误,请重新输入");
		}

		tbBaseinfoSpytMapper.update(entity);
		return R.ok(EditDto.closeAndReload());
	}

	/**
	 * 删除
	 */
	@Override
	@Transactional
	public R deleteTbBaseinfoSpyt(Long id) throws Exception{
		diySqlMapper.deleteTbBaseinfoSpyt(id);
		return R.ok(EditDto.closeAndReload());

	}

}